﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using Peak.Core;
using Peak.Core.DataInterfaces;

namespace Peak.Data
{
  public class InitDb
  {
    private readonly IUserRepository _userRepository;
    private readonly IProjectRepository _projectRepository;

    public InitDb(IUserRepository userRepository, IProjectRepository projectRepository)
    {
      _userRepository = userRepository;
      _projectRepository = projectRepository;
    }

    public void Initialize()
    {
      using (ITransaction transaction = SharpArch.Data.NHibernate.NHibernateSession.Current.BeginTransaction())
      {        
        User u = AddAdmin();
        AddSampleProject(u);
        transaction.Commit();
      }
    }

    private void AddSampleProject(User user)
    {
      Project p = new Project();
      p.Name = "Sample project";
      p.Users.Add(user);
      
      _projectRepository.SaveOrUpdate(p);
    }

    private User AddAdmin()
    {
      User user = new User();
      user.UserName = "Admin";
      user.SetPassword("password");      
      return _userRepository.Save(user);
    }
  }
}
